﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNS.Models
{
    public class MaNgachModels
    {
        public static BangMaNgach GetChiTietMaNgachMaNgach(string strMaNgach)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSMaNgach
                         where s.MaNgach == strMaNgach
                         select s).FirstOrDefault();
            return query;

        }
        public static BangMaNgach GetChiTietMaNgach(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSMaNgach
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }
        public static BangMaNgach GetDSMaNgach(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSMaNgach
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }
        public static List<BangMaNgach> GetMaNgachTheoNhomNgach()
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from dt in context.DSMaNgach
                        join ll in context.DSNhomNgach on dt.IDNhomNgach equals ll.ID
                        //where dt.ID == id
                        select dt;
            return query.ToList();
        }
        public static void Add(BangMaNgach mn)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            context.DSMaNgach.AddObject(mn);
            context.SaveChanges();
        }
        public static void Update(BangMaNgach id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSMaNgach
                        where h.ID == id.ID
                        select h;
            BangMaNgach kq = query.First<BangMaNgach>();
            kq = id;
            context.DSMaNgach.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
        public static void Delete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from h in context.DSMaNgach
                         where h.ID == id
                         select h).FirstOrDefault();

            context.DSMaNgach.DeleteObject(query);
            context.SaveChanges();

        }
    }
}